/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

import java.util.List;

/**
 * ClientUser object for the 'p4 depots' command.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 * 
 *         TODO: Introduce IDepotsResult
 */
public class Depots extends AbstractPerforceCommand implements IPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the result of the operation. */
  private List<String> result = Lists.newArrayList();

  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Convenience method for creating instances of Depots.
   * 
   * @param monitor for progress feedback.
   * @return the new Depots instance.
   */
  public static final Depots newInstance(IProgressMonitor monitor) {
    MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
    return new Depots(status, monitor);
  }

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Creates a new instance.
   * 
   * @param status the MultiStatus.
   * @param monitor for progress feedback.
   */
  private Depots(MultiStatus status, IProgressMonitor monitor) {
    super(status, monitor);
  }

  // -------------------------------------------------------- Public Methods
  // (IClientUserDelegate)

  @Override
  public void outputInfo(char level, String infoString) {
    if (infoString.startsWith("Depot ")) {
      int start = infoString.indexOf(' ') + 1;
      int stop = infoString.indexOf(' ', start);

      result.add("//" + infoString.substring(start, stop)); //$NON-NLS-1$

      getProgressMonitor().subTask("//" + infoString.substring(start, stop)); //$NON-NLS-1$
      getProgressMonitor().worked(1);
    } else {
      IStatus tempStatus = PerforceStatus.decode(infoString, IStatus.INFO);
      getMultiStatus().merge(tempStatus);
    }
  }

  // ----------------------------------------------------------- Public Methods
  // (IPerforceCommand)

  public String getCommand() {
    return "depots"; //$NON-NLS-1$
  }

  @Override
  public IStatus getStatus() {
    return getMultiStatus();
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Returns the result of the operation.
   * 
   * @return array of depot names
   */
  public String[] getResult() {
    // When the perforce admin didn't create a depot explicitly, perforce will
    // have on named
    // 'depot' implicitly. Unfortunately, this isn't returned by the 'depots'
    // command, so
    // we just fake the result.
    return result.isEmpty() ? new String[] {"//depot"} : result.toArray(new String[result.size()]); //$NON-NLS-1$
  }
}
